package sparqlclient;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class SparqlRequest {
	
	private SparqlClient client;

	public SparqlRequest() {
		super();
		this.client = new SparqlClient("127.0.0.1:3030/space");
	}
	
	
	public ArrayList<String> getSubClass(String word)
	{
		ArrayList<String> ret = new ArrayList<>();
		
		String queryString = "PREFIX : <http://ontologies.alwaysdata.net/space#>" +
				"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
				"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
				"PREFIX owl:  <http://www.w3.org/2002/07/owl#>" +
				"PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>" +
				"SELECT ?syno WHERE {" +
				"  ?main rdfs:label \""+word+"\"@fr ." +
				"  ?s rdfs:subClassOf ?main ." +
				"  ?s rdfs:label ?syno" +
				"  FILTER ( ?syno != \""+word+"\"@fr  )" +
				"}" +
				"LIMIT 200";
		Iterable<Map<String, String>> results = this.client.select(queryString);
		Iterator<Map<String, String>> itr = results.iterator();
		Map<String,String> res;
		while(itr.hasNext())
		{
			res = itr.next();
			for (String v: res.values())
				ret.add(v);
		}
		
		return ret;
	}
	
	/**
	 * Recupere les instances de sous classe du mot en param 
	 * @param word
	 */
	public ArrayList<String> getSynonym(String word)
	{
		ArrayList<String> ret = new ArrayList<>();
		
		String queryString = "PREFIX : <http://ontologies.alwaysdata.net/space#>\n" +
				"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
				"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
				"PREFIX owl:  <http://www.w3.org/2002/07/owl#>\n" +
				"PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>\n" +
				"SELECT ?syn WHERE{\n" +
					"?Obj rdfs:label ?syn.\n" +
					"?Obj rdfs:label \""+word+"\"@fr.\n"+
					"FILTER ( ?syn != \""+word+"\"@fr  )\n"+
				"}";
		
				  
		Iterable<Map<String, String>> results = this.client.select(queryString);
		Iterator<Map<String, String>> itr = results.iterator();
		Map<String,String> res;
		while(itr.hasNext())
		{
			res = itr.next();
			for (String v: res.values())
				ret.add(v);
		}
		
		return ret;
	}
	
	public ArrayList<String> getInstance(String word)
	{
		ArrayList<String> ret = new ArrayList<>();
		
		String queryString = "PREFIX : <http://ontologies.alwaysdata.net/space#>\n" +
				"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
				"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
				"PREFIX owl:  <http://www.w3.org/2002/07/owl#>\n" +
				"PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>\n" +
				"SELECT ?Obj WHERE {" +
					"?Cl rdf:type ?class." +
					"?Cl rdfs:label \""+word+"\"@fr." +
					"?Ins rdf:type ?Cl." +
					"?Ins rdfs:label ?Obj" +
				"} GROUP BY ?Obj";
		
		Iterable<Map<String, String>> results = this.client.select(queryString);
		Iterator<Map<String, String>> itr = results.iterator();
		Map<String,String> res;

		while(itr.hasNext())
		{
			res = itr.next();
			for (String v: res.values())
			{
				ret.add(v);
				//System.out.println(v);
			}
		}
		
		return ret;
	}
	
	

}
